package org.atovio.coolexam.mapper;

import org.apache.ibatis.annotations.*;
import org.atovio.coolexam.entity.Question;

import java.util.List;
import java.util.Map;

public interface QuestionMapper {

    @Insert("""
            insert into t_question (id, question_type,subject_id, content, score, difficulty_level,
            correct_answer)
            values (#{id},#{questionType},#{subjectId},#{content},#{score},#{difficultyLevel},
            #{correctAnswer});
            """)
    int insert(Question question);

    @Select("select * from t_question;")
    List<Question> selectAll();

    @Select("select * from t_question where id = #{id};")
    Question selectById(Integer id);

    @Select("select * from t_question where question_type = #{questionType};")
    List<Question> selectByQuestionType(Integer questionType);

    @Select("select * from t_question where question_type = #{questionType} order by rand();")
    List<Question> selectByQuestionTypeOnRandom(Integer questionType);

    @Select("select * from t_question where content = #{content};")
    Question selectByContent(String content);

    @Select("select * from t_question where difficulty_level = #{difficultyLevel};")
    List<Question> selectByDifficultyLevel(Integer difficultyLevel);

    /**
     * 根据自定义条件查询记录条数
     *
     * @param criteria 自定义条件
     * @return 记录条数
     */
    int selectCountSelective(@Param("criteria") Map<String, Object> criteria);

    List<Question> selectSelective(@Param("criteria") Map<String, Object> criteria);

    @Update("""
            update t_question set question_type = #{questionType},correct_answer = #{correctAnswer},
            content = #{content},score = #{score},difficulty_level = #{difficultyLevel} where id = #{id};""")
    int update(Question question);

    @Update("update t_question set question_type = #{questionType} where id = #{id};")
    int updateQuestionTypeById(Integer id, Integer questionType);

    @Update("update t_question set content = #{content} where id = #{id};")
    int updateContentById(Integer id, String content);

    @Update("update t_question set score = #{score} where id = #{id};")
    int updateScoreById(Integer id, Integer score);

    @Update("update t_question set difficulty_level = #{difficultyLevel} where id = #{id};")
    int updateDifficultyLevelById(Integer id, Integer difficultyLevel);

    @Update("update t_question set correct_answer = #{correctAnswer} where id = #{id};")
    int updateCorrectAnswerById(Integer id, Integer correctAnswer);

    @Delete("delete from t_question where id = #{id};")
    int deleteById(Integer id);
}
